package com.kadron.img;

import java.awt.image.BufferedImage;
import java.awt.image.RasterFormatException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.kadron.common.KUtil;

public class ImageHelper {

	private static final Log log = LogFactory.getLog(ImageHelper.class);
	
	private static final String[] contentTypes = {"image/png","image/jpg","image/JPG","image/JPEG","image/gif","image/bmp"};
	
	public static BufferedImage cropImage(BufferedImage image, 
			int xPosition,
			int yPosition, 
			int cropWidth, 
			int cropHeight) {
		BufferedImage croped = null;
		if (image != null 
				&& (image.getHeight() >= cropHeight || image.getWidth() >= cropWidth) 
				&& !(image.getHeight() == cropHeight && image.getWidth() == cropWidth)) {
			try {
				int x = xPosition;
				int y = yPosition;
				int w = cropWidth;
				int h = cropHeight;
				croped = image.getSubimage(x, y, w, h);
			} catch (RasterFormatException rfe) {
				throw rfe;
			}
		}
		return croped;
	}
	
	public static boolean checkContentType(String type) {
		if (KUtil.isEmpty(type)) {
			if (log.isDebugEnabled()) {
				log.debug("type is empty");
			}
			return false;
		}
		for (String contentType : contentTypes) {
			if (type.equalsIgnoreCase(contentType)) {
				return true;
			}
		}
		return false;
	}
	
}
